// pages/homelist/homelist.js
//获取应用实例
var app = getApp();
//引用
var fileData = require('../../utils/data.js')
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    // banner_url: fileData.getBannerData(),
    userId: '',
    userInfo: '',
    interval: 3000,
    duration: 1000,
    vertical: false,
    indicatordots: true,
    autoplay: true,
    indicatored: '#F08631',
    indicator: '#fff',
    navTopItems: fileData.getIndexNavData(),
    curNavId: 1,
    curIndex: 0,
    colors: ["red", "orange", "yellow", "green", "purple"],
    focus: '',
    // advert_url: fileData.getAdvertData(),
    actiItem: [],
    isMine:false,
    typeId:'',
    currentPage: 1,
    hasData: true,
    loadSuccess: false

  },
  bingfocusFunc: function (e) {
    this.setData({
      focus: 'focus'
    })
  },
  bingblurFunc: function (e) {
    this.setData({
      focus: ''
    })
  },
  getTypeList: function(){
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyActivity/getActivityDetailWithTypeIdByPage',
      data: { 
        typeId: _this.data.typeId, 
        userId: _this.data.userId,
        currentPage: this.data.currentPage,
        pageNumber: '10'
        },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if(res.data.success){
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          for (var i = 0; i < res.data.data.activities.length; i++) {
            res.data.data.activities[i].releaseTime = util.formatTime(res.data.data.activities[i].releaseTime);
            _this.data.actiItem.push(res.data.data.activities[i])
          }
          _this.setData({
            actiItem: _this.data.actiItem
          })
          if (res.data.data.page.totalNumber == _this.data.actiItem.length) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  getList: function (flag) {
    var _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyActivity/getAllActivityDetailsByPage',
      data: {
        userId: this.data.userId,
        name: '',
        currentPage: this.data.currentPage,
        pageNumber: '10'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideLoading();
        _this.setData({
          loadSuccess: true
        })
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
        } else {
          _this.setData({
            loadMore: false
          });
        }
        if (res.data.data.activities) {
          for (var j = 0; j < res.data.data.activities.length; j++) {
            res.data.data.activities[j].releaseTime = util.formatTime(res.data.data.activities[j].releaseTime)
            _this.data.actiItem.push(res.data.data.activities[j])
          }
          _this.setData({
            actiItem: _this.data.actiItem
          })
        }
        if (res.data.data.page.totalNumber == _this.data.actiItem.length) {
          _this.setData({
            hasData: false
          })
        }
      }

    })
  },
  // 关注/取消关注
  onunFollowFunc: function (e) {
    let id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var status = e.currentTarget.dataset.status;
    let _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyAttention/deleteAttentionRecordByParams',
      data: { userId: _this.data.userId, sourceId: id, block: 2 },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          _this.data.actiItem[index][status] = false;
          _this.setData({
            actiItem: _this.data.actiItem
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })

  },
  onFollowFunc: function (e) {
    let id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var status = e.currentTarget.dataset.status;
    let _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyAttention/save',
      data: { userId: _this.data.userId, sourceId: id, block: 2 },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: '关注成功',
            icon: 'success',
            duration: 2000
          })
          _this.data.actiItem[index][status] = true;
          _this.setData({
            actiItem: _this.data.actiItem
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 报名/取消报名
  onunSignFunc: function (e) {
    let id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var status = e.currentTarget.dataset.status;
    let _this = this;
    // wx.request({
    //   url: app.globalData.httpApi + 'outInterface/TGyUserInterController/doDeleteJoined.do',
    //   data: { userId: _this.data.userId, activityId: id},
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   success: function (res) {
    //     if (res.data.success) {
    //       _this.data.actiItem[index][status] = false;
    //       _this.setData({
    //         actiItem: _this.data.actiItem
    //       })
    //     }
    //   }
    // })
  },
  onSignFunc: function (e) {
    let item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    var status = e.currentTarget.dataset.status;
    if (item.fee > 0) {
      this.payMoney(item)
    }
    else {
      this.enroll(item.id);
    }
  },
  payMoney: function (item) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyActivity/payActivityFee',
      data: {
        orderId: item.id,
        openId: app.globalData.openId,
        userId: this.data.userInfo.id,
        money: item.fee,
        body: '活动报名费用'
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          that.requestPayment(res.data.data, item.id);
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  requestPayment: function (obj, id) {
    var that = this;
    wx.requestPayment({
      'timeStamp': obj.timeStamp,
      'nonceStr': obj.nonceStr,
      'package': obj.package,
      'signType': obj.signType,
      'paySign': obj.paySign,
      'success': function (res) {
        that.enroll(id)
      },
      'fail': function (res) {
        wx.showModal({
          title: '提示',
          content: '支付失败',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
            }
          }
        })
      }
    })
  },
  enroll: function (id) {
    let _this = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyActivityJoin/joinActivity',
      data: { userId: _this.data.userId, activityId: id },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: '报名成功',
            icon: 'success',
            duration: 2000
          })
          _this.data.actiItem[index][status] = true;
          _this.setData({
            actiItem: _this.data.actiItem
          })
        }
        else{
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 查看活动详情页
  openDetailPage: function (e) {
    wx.navigateTo({
      url: "../../pages/activeDetail/activeDetail?id=" + e.target.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: JSON.parse(wx.getStorageSync('userInfo')).id,
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if(options.id == undefined){
      
    }
    else{
      this.setData({
        typeId: options.id
      });
      wx.setNavigationBarTitle({
        title: options.title,
      })
     
    }
    
    // this.setData({ weatherApikey: getApp().globalData.weatherApikey });
    // this.loadLocation();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      currentPage: 1,
      actiItem: [],
      hasData: true
    })
    if (this.data.typeId == undefined) {
      this.getList()
    }
    else {
      this.getTypeList()
    }
    wx.showLoading({})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      currentPage: 1,
      actiItem: [],
      hasData: true
    })
    if (this.data.typeId == undefined) {
      this.getList()
    }
    else {
      this.getTypeList()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasData) {
      this.setData({
        currentPage: this.data.currentPage + 1
      })
      if (this.data.typeId == undefined) {
        this.getList()
      }
      else {
        this.getTypeList()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})